<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class fields extends MX_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('business_field');
        $this->load->library('pagination');
        date_default_timezone_set('Asia/Ho_Chi_Minh');
    }

    function index($row = 0) {
        $this->user->check_permission();

        include 'paging.php';
        $config['per_page'] = 2;
        $config['base_url'] = base_url() . 'administrator/fields/index/';

        $field_lst = $this->business_field->get(-1, $config['per_page'], $row);
        $config['total_rows'] = $this->business_field->total_count();
        $config['cur_page'] = $row;

        $this->pagination->initialize($config);

        $data['list_link'] = $this->pagination->create_links();
        $data['list_fields'] = $field_lst;

        $data['view'] = 'fields_index';
        $this->load->view('template', $data);
    }

    function create() {
        $this->user->check_permission();
        if ($this->input->post('title')) {
            $name = $this->input->post('title');
            $description = $this->input->post('description');
            $content = $this->input->post('content');
            $permalink = $this->business_field->generate_permalink($name);
            $image = $this->input->post('image');

            if ($this->business_field->insert($name, $description, $content, $permalink, $image)) {
                $this->session->set_flashdata('insert_field_success', true);
            } else {
                $this->session->set_flashdata('insert_field_error', true);
            }
            redirect('administrator/fields/create');
        }

        $data['view'] = 'fields_create';
        $this->load->view('template', $data);
    }

    function edit($id) {
        $this->user->check_permission();

        if ($this->input->post('title')) {
            $id = $this->input->post('id');
            $name = $this->input->post('title');
            $description = $this->input->post('description');
            $content = $this->input->post('content');
            $permalink = $this->business_field->generate_permalink($name);
            $image = $this->input->post('hdffeatured_image');

            if ($this->business_field->edit($id, $name, $description, $content, $permalink, $image)) {
                $this->session->set_flashdata('update_pages_success', true);
            } else {
                $this->session->set_flashdata('update_pages_error', true);
            }
            redirect('administrator/fields/edit/' . $id);
        }

        $field = $this->business_field->get($id);
        $data['field'] = $field;

        $data['view'] = 'fields_edit';
        $this->load->view('template', $data);
    }

    function delete() {
        $this->user->check_permission();

        $id = $this->input->post('id');
        if ($this->business_field->delete($id)) {
            echo json_encode(array('result' => TRUE));
        } else {
            echo json_encode(array('result' => FALSE));
        }
    }

}

?>
